//
//  HomeMainAction.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "HomeMainAction.h"
#import "HomeCreditCardModel.h"
//#import "ThirdPartHelper.h"
//#import "ThirdPartyManager.h"
NSString * const homeLoanApplyCacheKey = @"homeCardApplyModel";
NSString * const homeCreditCardApplyCacheKey = @"homeCreditCardApplyCacheKey";
@interface homeCardApplyModel : NSObject
@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * applyCount;
@end

@implementation homeCardApplyModel
@end


static id getMetaData(){
    
    static NSArray * metaArray = nil;
    metaArray = @[@[@"小额贷款",@"1000-50000"],
                  @[@"大额贷款",@"50000以上"],
                  @[@"企业贷款",@"客户全程服务"]];
    
    return metaArray;
}

static id getMetaImageData(){
    
    static NSArray * imageArray = nil;
    imageArray = @[UI_IMAGE_HOME_ICON_LOAN_S,UI_IMAGE_HOME_ICON_LOAN_B,UI_IMAGE_HOME_ICON_LOAN_COMPANY];
    return imageArray;
}

static id getMetaCollectionImageData(){
    
    static NSArray * collectionImageArray = nil;
    collectionImageArray = @[UI_IMAGE_HOME_ICON_BANNER,UI_IMAGE_HOME_ICON_BANNER_CARD];
    return collectionImageArray;
}

@interface HomeMainAction()
@property (nonatomic,strong)NSMutableArray * applyDataArray;
@end

@implementation HomeMainAction
-(instancetype)init{
    
    if (self = [super init]) {
        
        self.dataArray = [NSMutableArray arrayWithCapacity:6];
        self.tableView = nil;
    }
    
    return self;
}


-(void)start{
    [self applySuccessCount];
    [self getCreditCardCache];
    
    id cacheData = [HomeCreditCardModel getCreditCardCache];
    if(cacheData){
        [self.dataArray addObjectsFromArray:cacheData];
    }
    [self startGetCreditCardData];
    
}
-(void)startGetCreditCardData{
    
    [[ClientApi getInstance] getCreditCardLineBanks:8 withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        if(error){
            
            GYNLog(@"error = %@",[error description]);
        }else{
            //单独存一份
            [NSObject cacheData:responseObject withCacheKey:homeCreditCardApplyCacheKey];
            
            homeCardApplyModel * applyModel = self.applyDataArray[1];
            applyModel.applyCount = [responseObject valueForKey:@"apply_count"];
            NSArray * array = [HomeCreditCardModel getCreditCardList:[responseObject valueForKey:@"items"]];
            if(array){
                [HomeCreditCardModel cacheCreditCardData:[responseObject valueForKey:@"items"]];
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:array];
                
                if(self.collectionView){
                    
                    [self.collectionView reloadData];
                }
            }
        }
        
    }];
    
    
}

- (void)getCreditCardCache{
    id creditCache = [NSObject getCacheDataWithKey:homeCreditCardApplyCacheKey];
    if(creditCache){
        
        homeCardApplyModel * applyModel = self.applyDataArray[1];
        NSString *apply_count = [creditCache valueForKey:@"apply_count"];
        applyModel.applyCount = apply_count;
        
    }
    
}
/***  首页贷款、信用卡申请人数*/
- (void)applySuccessCount{
    
    __weak typeof(self)weakSelf = self;
    void(^dealingResult)(id objc) = ^(id result){
        
        
        homeCardApplyModel * applyModel = self.applyDataArray[0];
        applyModel.applyCount = [NSString stringWithFormat:@"%@",result[@"total"]];
        
        NSString * tempCountInfo = [NSString stringWithFormat:@"%@人完成贷款申请",applyModel.applyCount];
        
        if(weakSelf.refreshApplySuccessBlock){
            weakSelf.refreshApplySuccessBlock(tempCountInfo);
        }
    };
    
    //1.检查本地缓存
    id countCache = [NSObject getCacheDataWithKey:homeLoanApplyCacheKey];
    if(countCache){
        //        GYNLog(@"贷款申请人数cache:%@",countCache);
        dealingResult(countCache);
        [weakSelf refreshApplyCardCountWithIndex:0];
    }
    
    [[ClientApi getInstance] getHomeApplyCardSuccessWithUnite:nil size:nil withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if(error){
            GYNLog(@"贷款申请人数error:%@",error);
            //1.检查本地缓存
            id countCache = [NSObject getCacheDataWithKey:homeLoanApplyCacheKey];
            if(countCache){
                dealingResult(countCache);
            }
            return;
        }
        [NSObject cacheData:responseObject withCacheKey:homeLoanApplyCacheKey];
        dealingResult(responseObject);
    }];
    
}

- (void)refreshApplyCardCountWithIndex:(NSUInteger)index{
    __weak typeof(self)weakSelf = self;
    NSString * tempCount = @"0";
    if(index>=self.applyDataArray.count) return;
    homeCardApplyModel * model = self.applyDataArray[index];
    tempCount = model.applyCount;
    
    if(weakSelf.refreshApplySuccessBlock){
        weakSelf.refreshApplySuccessBlock(tempCount);
    }
}


// realease resource ,please be careful
-(void)dealloc{
    
}


+(id)getLoanMetaData{
    
    NSArray * metaArray = getMetaData();
    return metaArray;
}

+(id)getLoanMetaImageData{
    
    NSArray * metaArray = getMetaImageData();
    return metaArray;
    
}

+(id)getMetaCollectionImageData{
    
    NSArray * metaArray = getMetaCollectionImageData();
    return metaArray;
    
}

-(NSInteger)getCreditCardRowCount{
    
    return [self.dataArray count];
}
-(id)getCreditCardData{
    
    return self.dataArray;
}

-(void)navLoanController:(id)nav withIndex:(NSInteger) index{
    
    GYNLog(@"各种导入还没有处理导航栏推入");
    
//    if(![BaseAction isLogin]){
//        
//        id loginController = [LoginMainController createNav];
//        [nav presentViewController:loginController animated:YES completion:nil];
//        
//        return;
//    }
//    NSString * recordID = nil;
//    if (index ==0) {
//        recordID = U_Home_smallLoan_ID;
//    }else if (index ==1){
//        recordID = U_Home_bigLoan_ID;
//    }else if (index ==2){
//        recordID = U_Home_enterPriseLoan_ID;
//    }
//    if(recordID){
//        [[ThirdPartyManager getInstance] logUmengEvent:recordID];
//    }
//    
//    HomeLoanController * loanController = [[HomeLoanController alloc] init];
//    [nav pushViewController:loanController animated:YES];
}

-(void)navCreditController:(id) nav withIndex:(NSInteger) index{
    HomeCreditCardModel *cardModel = [_dataArray objectAtIndex:index];
    //    HomeCreditCardModel *copyModel = [HomeCreditCardModel copyWithModel:cardModel];
    
    if (index == [self.dataArray count] - 1) { //更多银行
//        [[ThirdPartyManager getInstance] logUmengEvent:U_Home_moreBankBtn_ID];
        GYNLog(@"原本是  HomeCreditCardController")
//        HomeCreditCardController * creditController = [[HomeCreditCardController alloc] init];
//        creditController.hidesBottomBarWhenPushed = YES;
//        [nav pushViewController:creditController animated:YES];
    }else{ //银行
        
//        [ThirdPartHelper recordUMengWithName:cardModel.bank_name];
        GYNLog(@"原本是  CreditCardListController");
//        CreditCardListController *creditCardListController = [[CreditCardListController alloc] init];
//        creditCardListController.creditCard = cardModel;
//        [nav pushViewController:creditCardListController animated:YES];
    }
    
}


+ (NSArray *)getMainTopViewData{
    return @[@"贷款",@"信用卡"];
}

- (NSString *)getApplicationSuccessInfoWithIndex:(NSUInteger )index{
    if(index>self.applyDataArray.count) return @"0";
    homeCardApplyModel * model = self.applyDataArray[index];
    return model.applyCount;
}

- (NSMutableArray *)applyDataArray{
    if (!_applyDataArray) {
        _applyDataArray = [[NSMutableArray alloc] init];
        NSArray * titles = [HomeMainAction getMainTopViewData];
        for (NSString * title in titles) {
            homeCardApplyModel * model = [[homeCardApplyModel alloc] init];
            model.title = [title copy];
            model.applyCount =@"0";
            [_applyDataArray addObject:model];
        }
    }
    return _applyDataArray;
}

@end
